<?php
class XWithinPointcut extends XAbstractPointcut{
	protected $filePattern = null;
	
	public function __construct($source, $method, $pattern){
		parent::__construct($source, $method, $pattern);		
		if(preg_match("/^within\((.+)\)/i", $this->pattern, $match)){			
			$this->filePattern = $match[1];
			if(!preg_match("/^project\./i", $this->filePattern)){
				$this->filePattern = "project.".trim($this->filePattern, "./\\");
			}
			$this->filePattern = str_replace("..", "[\w\.]+", $this->filePattern);
			$this->filePattern = preg_replace("/(\w+)\./", "\${1}\.", $this->filePattern);
			$this->filePattern = str_replace("*", "\w+", $this->filePattern);
		}
	}
	
	public function match($class, $method){
		$reflection = XClassLoader::loader($class)->reflection();
		$file = trim(str_replace(PROJECT_ROOT, "", $reflection->getFileName()), "/\\");
		$file = "project.".preg_replace("/[\/\\\]/i", ".", $file);
		if(!preg_match("/{$this->filePattern}/i", $file)){
			return false;
		}
		return true;
	}
}
?>